<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link href="/css/adminMenu.css" rel="stylesheet" type="text/css" />
	<script type="text/javascript" language="javascript" src="/js/jquery-1.6.1.min.js"></script>
	<script type="text/javascript" language="javascript" src="/js/ajaxfileupload.js"></script>
	<script type="text/javascript" language="javascript" src="/js/tool.js"></script>
	<title>琥珀川 - 添加电影</title>
	<style type="text/css">
		/*资源类型图标Div*/
		.sourceTypeLogoDiv{
			width:20px;
			height:20px;
			background-repeat:no-repeat;
			background-image:url(/image/system/sourceTypeLogo.png);
		}
	</style>
</head>

<body>
	<jsp:include page="/admin/include/menu" flush="true"/>
	<div class="mainDiv">
		<table border="1">
			<tr>
				<td align="right">片名:</td>
				<td><input id="name" type="text" /><A href="javascript:clickSeekName()">[检索]</A></td>
				
				<td align="right">导演:</td>
				<td><input id="director" type="text"/></td>
				
				<td rowspan="6" align="right">海报:</td>
				<td rowspan="6">
					<div style="width:134px; height:178px; overflow:hidden; border:1px solid black;">
						<img id="logoImg" width="134" src="/image/system/defaultMovieLogo.png"/>
					</div>
					<div>
						<input id="logoFile" name="logoFile" type="file" onchange="changeLogoPath()" style="width:150px;"/>
						<input id="tempImgName" type="hidden"/>
					</div>
				</td>
			</tr>
			<tr>
				<td align="right">别名:</td>
				<td><input id="byname" type="text" /></td>
				<td rowspan="5" align="right">演员:</td>
				<td rowspan="5">
					<textarea id="actor" wrap="off" style="width:400px; height:200px;"></textarea>
					<br/>
					<input id="replaceSpace" type="button" value="替换 为\t" onclick="clickReplaceSpace()"/>
				</td>
			</tr>
			<tr>	
				<td align="right">类型:</td>
				<td><input id="type" type="text" /></td>
			</tr>
			<tr>
				<td align="right">地区:</td>
				<td><input id="area" type="text" /></td>
			</tr>
			<tr>
				<td align="right">上映时间:</td>
				<td><input id="releaseDate" type="text" /></td>
			</tr>
			<tr>
				<td align="right">百科网址:</td>
				<td>
					<input id="baikeUrl" type="text" />
					<A href="javascript:clickOpenSearchPage('http://baike.baidu.com/search?word=')">[搜索]</A>
				</td>
			</tr>
			<tr>
				<td align="right">剧情简介:</td>
				<td colspan="5">
					<textarea id="summary" style="width:800px; height:200px;"></textarea>
				</td>
			</tr>
			<tr>
				<td>
				</td>
				<td colspan="5">
					<table border="1">
						<tr>
							<td></td>
							<td></td>
							<td></td>
							<td>搜索</td>
							<td>状况</td>
							<td>网址</td>
							<td>密码</td>
						</tr>
						<%--搜索资源网址--%>
						<c:forEach items="${sourceTypeList}"  varStatus="status" var="aSourceType">
							<tr>
								<td>${status.index}</td>
								<td>
									${aSourceType.id}
									<input id="sourceTypeId_${status.index}" type="hidden" value="${aSourceType.id}"/>
								</td>
								<td>
									<div class="sourceTypeLogoDiv" style="background-position:0px ${aSourceType.logoPositionY};"></div>
								</td>
								<td>
									<A href="javascript:clickOpenSearchPage('${aSourceType.searchUrl}')">
										${aSourceType.name}
									</A>
								</td>
								<%--添加资源状况ID--%>
								<td>
									<select id="sourceModeId_${status.index}">
										<option value="0">请选择</option>
										<c:forEach items="${sourceModeList}" var="aSourceMode">
											<option value="${aSourceMode.id}">${aSourceMode.name}</option>
										</c:forEach>
									</select>
								</td>
								<td>
									<input id="sourceUrl_${status.index}" type="text" style="width:400px;"/>
								</td>
							</tr>
						</c:forEach>
					</table>
					<input id="sourceTypeCount" type="hidden" value="${fn:length(sourceTypeList)}"/>
				</td>
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td colspan="5"><input id="submitButton" type="button" value="提交" onclick="clickSubmitButton()"/></td>
			</tr>
		</table>
	</div>
</body>
<script type="text/javascript">
	<%--改变文件路径事件--%>
	function changeLogoPath(){
		tool.fileUpload({
			requestUrl:"/admin/ajax?action=uploadImg",
			overtimeUrl:"/admin/login",
			fileId:"logoFile",
			success:function(msg){
				if(msg.stateCode == "success"){
					$("#logoImg").attr("src","/image/temp/"+msg.tempImgName);
					$("#tempImgName").val(msg.tempImgName);
				}
				else if(msg.stateCode == "not_upload"){
					alert("没有上传海报");
				}
				else if(msg.stateCode == "error"){
					alert("上传错误,可能是格式错误");
				}
			}
		});
	}
	
	<%--点击替换空格为Tab按钮--%>
	function clickReplaceSpace(){
		var actor = $("#actor").val();
		actor = replaceAll(actor," ","\t");
		$("#actor").val(actor);
	}
	
	<%--替换str里全部字符--%>
	function replaceAll(str,str1,str2){
		var oldStr = str;
		while(true){
			var newStr = oldStr.replace(str1,str2);
			if(newStr == oldStr){
				break;
			}
			else{
				oldStr = newStr;
			}
		}
		return oldStr;
	}
	
	<%--点击打开搜索页面--%>
	function clickOpenSearchPage(searchUrl){
		var name = $("#name").val();
		window.open(searchUrl+encodeURI(name));
	}
	
	<%--点击提交按钮事件--%>
	function clickSubmitButton(){
		var sourceTypeCount = parseInt($("#sourceTypeCount").val());
		var dataSet = {
			"name":$("#name").val(),
			"byname":$("#byname").val(),
			"tempImgName":$("#tempImgName").val(),
			"type":$("#type").val(),
			"director":$("#director").val(),
			"area":$("#area").val(),
			"releaseDate":$("#releaseDate").val(),
			"baikeUrl":$("#baikeUrl").val(),
			"summary":$("#summary").val(),
			"actor":$("#actor").val(),
			"sourceTypeCount":sourceTypeCount
		};
		for(var i = 0 ; i < sourceTypeCount ; i++){
			var sourceTypeId = $("#sourceTypeId_"+i).val();
			var sourceModeId = $("#sourceModeId_"+i).val();
			var sourceUrl = $("#sourceUrl_"+i).val();
			
			dataSet["sourceTypeId_"+i] = sourceTypeId;
			dataSet["sourceModeId_"+i] = sourceModeId;
			dataSet["sourceUrl_"+i] = sourceUrl;
		}
		
		tool.ajax({
			requestUrl:"/admin/ajax?action=addMovieInfo",
			overtimeUrl:"/admin/login",
			dataSet:dataSet,
			success:function(msg){
				if(msg.stateCode == "name_is_null"){
					alert("电影名不能为空");
				}
				else if(msg.stateCode == "name_exist"){
					alert("电影名已存在");
				}
				else if(msg.stateCode == "type_not_exist"){
					alert("类型不存在");
				}
				else if(msg.stateCode == "area_not_exist"){
					alert("地区不存在");
				}
				else if(msg.stateCode == "releaseDate_format_error"){
					alert("上映时间格式错误");
				}
				else if(msg.stateCode == "actor_format_error"){
					alert("演员格式错误");
				}
				else if(msg.stateCode == "success"){
					alert("添加成功");
				}
				else{
					alert(msg.stateCode);
				}
			}
		});
	}
	
	<%--点击搜索电影名事件--%>
	function clickSeekName(){
		var dataSet = {
			"name":$("#name").val()
		};
		tool.ajax({
			requestUrl:"/admin/ajax?action=addMovieSeekName",
			overtimeUrl:"/admin/login",
			dataSet:dataSet,
			success:function(msg){
				if(msg.stateCode == "name_is_null"){
					alert("电影名不能为空");
				}
				else if(msg.stateCode == "name_exist"){
					alert("电影名已存在");
				}
				else if(msg.stateCode == "success"){
					alert("电影名可用");
				}
				else{
					alert(msg.stateCode);
				}
			}
		});
	}
</script>
</html>
